home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / LIB / NDIROS2.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  8KB  |  245 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       n d i r o s 2 . c                                            */
  3. /*                                                                    */
  4. /*       UUPC/extended OS/2 directory search functions                */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: ndiros2.c 1.7 1993/10/03 20:37:34 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: ndiros2.c $
  24.  *     Revision 1.7  1993/10/03  20:37:34  ahd
  25.  *     Delete debugging message of returned value to make consistent with
  26.  *     other environment messages from ndir()
  27.  *
  28.  *     Revision 1.6  1993/09/20  04:39:51  ahd
  29.  *     OS/2 2.x support
  30.  *
  31.  *     Revision 1.5  1993/07/20  21:45:37  ahd
  32.  *     Clean up header
  33.  *
  34.  *     Revision 1.4  1993/04/05  12:26:01  ahd
  35.  *     Drop RCSID
  36.  *
  37.  *     Revision 1.3  1993/04/05  04:32:19  ahd
  38.  *     Add timestamp, size to information returned by directory searches
  39.  *
  40.  *     Revision 1.2  1993/04/04  19:35:14  ahd
  41.  *     Include time_t timestamp
  42.  *
  43.  */
  44.  
  45. /*--------------------------------------------------------------------*/
  46. /*    ndir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87      */
  47. /*    ndir.c for MS-OS2 by Drew Derbyshire (help@kendra.kew.com>,     */
  48. /*           April/91                                                 */
  49. /*                                                                    */
  50. /*         Berkeley-style directory reading routine on MS-OS2         */
  51. /*--------------------------------------------------------------------*/
  52.  
  53. /*--------------------------------------------------------------------*/
  54. /*                        System include files                        */
  55. /*--------------------------------------------------------------------*/
  56.  
  57. #include <stdio.h>
  58. #include <ctype.h>
  59. #include <string.h>
  60. #include <stdlib.h>
  61. #include <assert.h>
  62. #include <time.h>
  63.  
  64. /*--------------------------------------------------------------------*/
  65. /*                         OS/2 include files                         */
  66. /*--------------------------------------------------------------------*/
  67.  
  68. #define INCL_BASE
  69. #include <os2.h>
  70.  
  71. /*--------------------------------------------------------------------*/
  72. /*                    UUPC/extended include files                     */
  73. /*--------------------------------------------------------------------*/
  74.  
  75. #define FAMILY_API
  76.  
  77. #include "lib.h"
  78. #include "uundir.h"
  79. #include "dos2unix.h"
  80.  
  81. static HDIR dir_handle;
  82. static char *pathname = NULL;
  83.  
  84. #if defined(__OS2__)
  85.  
  86. #define FINDFIRST_LEVEL (FIL_STANDARD)    /* OS/2 2.x requires different
  87.                                              parameters to find first
  88.                                              file */
  89.  
  90. static FILEFINDBUF3 findbuf;
  91.  
  92. #else
  93.  
  94. #define FINDFIRST_LEVEL (0L)
  95. static FILEFINDBUF findbuf;
  96.  
  97. #endif
  98.  
  99. currentfile();
  100.  
  101. /*--------------------------------------------------------------------*/
  102. /*    o p e n d i r                                                   */
  103. /*                                                                    */
  104. /*    Open a directory                                                */
  105. /*--------------------------------------------------------------------*/
  106.  
  107. extern DIR *opendirx( const char *dirname, char *pattern)
  108. {
  109.  
  110.    DIR *dirp;
  111.  
  112. #ifdef __OS2__
  113.    APIRET rc;
  114.    ULONG count = 1L;
  115. #else
  116.    USHORT rc;
  117.    USHORT count = 1;
  118. #endif
  119.  
  120.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  121.    strcpy(pathname, dirname);
  122.  
  123.    if ((*pattern != '/') && (dirname[ strlen(dirname) - 1] != '/'))
  124.       strcat(pathname,"/");
  125.    strcat(pathname,pattern);
  126.    printmsg(5,"opendir: Opening directory %s", pathname );
  127.  
  128. /*--------------------------------------------------------------------*/
  129. /*                Read the first file in the directory                */
  130. /*--------------------------------------------------------------------*/
  131.  
  132.    dir_handle = HDIR_CREATE;
  133.    rc = DosFindFirst( pathname,
  134.             &dir_handle,
  135.             FILE_NORMAL,
  136.             &findbuf,
  137.             sizeof( findbuf ),
  138.             &count,
  139.             FINDFIRST_LEVEL );
  140.  
  141. /*--------------------------------------------------------------------*/
  142. /*            Process the return code from the first file             */
  143. /*--------------------------------------------------------------------*/
  144.  
  145.    if ( rc == 0 )
  146.    {
  147.       dirp = malloc( sizeof( DIR ));
  148.       dirp->dirfirst = 1;
  149.       strcpy(dirp->dirid, "DIR");
  150.       return dirp;
  151.    }
  152.    else {
  153.       if (( rc != ERROR_NO_MORE_FILES ) &&
  154.           ( rc != ERROR_PATH_NOT_FOUND))
  155.          printmsg(4,"opendir: Error %d on directory %s",
  156.                   (int) rc, pathname );
  157.       return NULL;
  158.    } /* else */
  159.  
  160. } /*opendir*/
  161.  
  162. /*--------------------------------------------------------------------*/
  163. /*    r e a d d i r                                                   */
  164. /*                                                                    */
  165. /*    Get next entry in a directory                                   */
  166. /*--------------------------------------------------------------------*/
  167.  
  168. struct direct *readdir(DIR *dirp)
  169. {
  170.  
  171. #if defined(__OS2__)
  172.    ULONG count = 1L;
  173.    APIRET rc = 0;
  174. #else
  175.    USHORT count = 1;
  176.    USHORT rc = 0;
  177. #endif
  178.  
  179.    if ( ! equal(dirp->dirid, "DIR" ))
  180.    {
  181.       printmsg(0,"readdir: No directory open to read");
  182.       panic();
  183.    }
  184.  
  185.    if (dirp->dirfirst)
  186.    {
  187.       printmsg(5,"readdir: Opening directory %s", pathname );
  188.       dirp->dirfirst = 0;
  189.    }
  190.    else
  191.       rc = DosFindNext( dir_handle,
  192.                &findbuf,
  193.                sizeof( findbuf ) ,
  194.                &count );
  195.  
  196.    if ( rc == 0 )
  197.    {
  198.       dirp->dirent.d_ino = -1;   /* no inode information */
  199.       strlwr(strcpy(dirp->dirent.d_name, findbuf.achName ));
  200.       dirp->dirent.d_namlen = findbuf.cchName;
  201.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  202.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  203.       dirp->dirent.d_modified = dos2unix( findbuf.fdateLastWrite,
  204.                                          findbuf.ftimeLastWrite );
  205.       dirp->dirent.d_size     = findbuf.cbFile;
  206.  
  207.       return &(dirp->dirent);
  208.    }
  209.    else {
  210.       if ( rc != ERROR_NO_MORE_FILES )
  211.          printmsg(0,"readdir: Error %d on directory %s",
  212.                   (int) rc, pathname );
  213.       return NULL;
  214.    } /* else */
  215.  
  216. } /*readdir*/
  217.  
  218. /*--------------------------------------------------------------------*/
  219. /*    c l o s e d i r                                                 */
  220. /*                                                                    */
  221. /*    Close a directory                                               */
  222. /*--------------------------------------------------------------------*/
  223.  
  224. void closedir(DIR *dirp)
  225. {
  226.    USHORT rc;
  227.  
  228.    if ( ! equal(dirp->dirid, "DIR" ))
  229.    {
  230.       printmsg(0,"closedir: No directory open");
  231.       panic();
  232.    }
  233.  
  234.    printmsg(5,"closedir: Closing directory %s", pathname );
  235.    rc = DosFindClose( dir_handle );
  236.    if ( rc != 0 )
  237.      printmsg(0,"closedir: Error %d on directory %s",
  238.               (int) rc, pathname );
  239.    free( dirp );
  240.    dirp = NULL;
  241.    free( pathname );
  242.    pathname = NULL;
  243.  
  244. } /*closedir*/
  245.